import pygame
import datetime
import sys
import os
import argparse

# 设置显示环境变量
os.environ['DISPLAY'] = ':0'
os.environ['XAUTHORITY'] = '/home/w-dingyu01@uos/.Xauthority'

class WallpaperGenerator:
    def __init__(self, output_path, width=1920, height=1080):
        self.output_path = output_path
        self.width = width
        self.height = height
        self.path = '/home/w-dingyu01@uos/dy外网/My_model/pygame测试/'
        
        # 初始化pygame
        pygame.init()
        
        # 创建surface
        self.surface = pygame.Surface((width, height))
        
        # 定义颜色
        self.COLORS = {
            'BLACK': (0, 0, 0),
            'WHITE': (255, 255, 255),
            'BLUE': (0, 120, 255),
            'DARK_BLUE': (30, 30, 60),
            'GRAY': (50, 50, 50),
            'LIGHT_BLUE': (173, 216, 230),
            'LIGHT_GREEN': (144, 238, 144),
            'ORANGE': (255, 165, 0),
            'LIGHT_GRAY': (211, 211, 211),
            'GREEN': (0, 200, 0),
            'YELLOW': (255, 255, 0),
            'RED': (255, 0, 0)
        }
        
        # 加载字体
        self.load_fonts()
        
        # 定义内容
        self.linux_shortcuts = [
            ("终端操作", [
                "Ctrl + C", "终止当前进程",
                "Ctrl + Z", "暂停当前进程",
                "Ctrl + D", "退出终端",
                "Ctrl + L", "清屏",
                "Ctrl + A", "移动到行首",
                "Ctrl + E", "移动到行尾",
                "Tab", "命令补全",
                "↑/↓", "命令历史"
            ]),
            ("文件操作", [
                "ls", "列出文件",
                "cd", "切换目录", 
                "cp", "复制文件",
                "mv", "移动文件",
                "rm", "删除文件",
                "mkdir", "创建目录",
                "chmod", "修改权限",
                "grep", "文本搜索"
            ]),
            ("系统管理", [
                "ps", "查看进程",
                "kill", "终止进程",
                "top", "系统监控",
                "df", "磁盘空间",
                "free", "内存使用",
                "sudo", "超级权限",
                "apt-get", "包管理",
                "systemctl", "服务管理"
            ])
        ]
        
        self.coding_tips = [
            "🔸 定期提交代码到版本控制",
            "🔸 编写清晰的注释和文档", 
            "🔸 遵循代码规范",
            "🔸 测试驱动开发",
            "🔸 代码重构保持简洁",
            "🔸 使用有意义的命名",
            "🔸 错误处理要完善",
            "🔸 性能优化要适度"
        ]
        
        self.daily_reminders = [
            "✅ 检查邮件和消息",
            "✅ 更新工作日志",
            "✅ 备份重要文件", 
            "✅ 整理桌面文件",
            "✅ 计划明日任务",
            "✅ 提交今日工作",
            "✅ 清理临时文件",
            "✅ 安全关闭系统"
        ]

    def load_fonts(self):
        """加载字体"""
        try:
            self.title_font = pygame.font.Font(os.path.join(self.path, 'Ubuntu_18.04_SimHei.ttf'), 48)
            self.section_font = pygame.font.Font(os.path.join(self.path, 'Ubuntu_18.04_SimHei.ttf'), 32)
            self.content_font = pygame.font.Font(os.path.join(self.path, 'Ubuntu_18.04_SimHei.ttf'), 24)
            self.small_font = pygame.font.Font(os.path.join(self.path, 'Ubuntu_18.04_SimHei.ttf'), 18)
        except:
            # 如果自定义字体加载失败，使用系统默认字体
            self.title_font = pygame.font.Font(None, 48)
            self.section_font = pygame.font.Font(None, 32)
            self.content_font = pygame.font.Font(None, 24)
            self.small_font = pygame.font.Font(None, 18)

    def draw_background(self):
        """绘制背景"""
        # 渐变背景
        for y in range(self.height):
            # 从深蓝到黑色的渐变
            color_ratio = y / self.height
            r = int(30 * (1 - color_ratio))
            g = int(30 * (1 - color_ratio))
            b = int(60 * (1 - color_ratio))
            pygame.draw.line(self.surface, (r, g, b), (0, y), (self.width, y))
        
        # 添加网格线
        grid_color = (40, 40, 80)
        for x in range(0, self.width, 50):
            pygame.draw.line(self.surface, grid_color, (x, 0), (x, self.height), 1)
        for y in range(0, self.height, 50):
            pygame.draw.line(self.surface, grid_color, (0, y), (self.width, y), 1)

    def draw_text_with_shadow(self, text, font, color, x, y, shadow_offset=2):
        """绘制带阴影的文字"""
        shadow = font.render(text, True, self.COLORS['GRAY'])
        text_surface = font.render(text, True, color)
        self.surface.blit(shadow, (x + shadow_offset, y + shadow_offset))
        self.surface.blit(text_surface, (x, y))
        return text_surface.get_rect(topleft=(x, y))

    def draw_linux_shortcuts(self):
        """绘制Linux快捷键部分"""
        x, y = 50, 100
        section_width = (self.width - 200) // 3
        
        for section_title, shortcuts in self.linux_shortcuts:
            # 绘制分区标题
            title_rect = self.draw_text_with_shadow(
                section_title, self.section_font, self.COLORS['LIGHT_BLUE'], 
                x, y
            )
            
            # 绘制快捷键内容
            content_y = y + 60
            for i in range(0, len(shortcuts), 2):
                # 快捷键
                self.draw_text_with_shadow(
                    shortcuts[i], self.content_font, self.COLORS['YELLOW'],
                    x, content_y
                )
                
                # 描述
                self.draw_text_with_shadow(
                    shortcuts[i+1], self.content_font, self.COLORS['WHITE'],
                    x + 150, content_y
                )
                
                content_y += 40
            
            # 添加分隔线
            pygame.draw.line(
                self.surface, self.COLORS['BLUE'],
                (x, content_y + 20), (x + section_width - 50, content_y + 20), 2
            )
            
            x += section_width

    def draw_coding_tips(self):
        """绘制编程提示部分"""
        x, y = 50, 400
        
        # 标题
        title_rect = self.draw_text_with_shadow(
            "编程最佳实践", self.section_font, self.COLORS['LIGHT_GREEN'], 
            x, y
        )
        
        # 内容
        content_y = y + 60
        for tip in self.coding_tips:
            self.draw_text_with_shadow(
                tip, self.content_font, self.COLORS['WHITE'],
                x, content_y
            )
            content_y += 35

    def draw_daily_reminders(self):
        """绘制日常提醒部分"""
        x, y = self.width // 2 + 50, 400
        
        # 标题
        title_rect = self.draw_text_with_shadow(
            "日常检查清单", self.section_font, self.COLORS['ORANGE'], 
            x, y
        )
        
        # 内容
        content_y = y + 60
        for reminder in self.daily_reminders:
            self.draw_text_with_shadow(
                reminder, self.content_font, self.COLORS['WHITE'],
                x, content_y
            )
            content_y += 35

    def draw_header_footer(self):
        """绘制页眉和页脚"""
        current_time = datetime.datetime.now()
        
        # 页眉 - 标题和日期
        header_bg = pygame.Rect(0, 0, self.width, 60)
        pygame.draw.rect(self.surface, (40, 40, 80, 180), header_bg)
        
        title_text = "Linux 系统实用信息壁纸"
        date_text = current_time.strftime("%Y-%m-%d %A")
        
        self.draw_text_with_shadow(
            title_text, self.title_font, self.COLORS['WHITE'], 20, 15
        )
        self.draw_text_with_shadow(
            date_text, self.section_font, self.COLORS['LIGHT_BLUE'], 
            self.width - 400, 20
        )
        
        # 页脚 - 生成信息
        footer_bg = pygame.Rect(0, self.height - 40, self.width, 40)
        pygame.draw.rect(self.surface, (40, 40, 80, 180), footer_bg)
        
        info_text = f"壁纸生成时间: {current_time.strftime('%H:%M:%S')} | 分辨率: {self.width}x{self.height}"
        self.draw_text_with_shadow(
            info_text, self.small_font, self.COLORS['LIGHT_GRAY'], 
            20, self.height - 30
        )

    def draw_decorative_elements(self):
        """绘制装饰元素"""
        # 在角落添加一些装饰性图形
        corners = [
            (0, 0), (self.width-100, 0), 
            (0, self.height-100), (self.width-100, self.height-100)
        ]
        
        for corner_x, corner_y in corners:
            # 绘制装饰性圆圈
            for i in range(3):
                radius = 20 + i * 15
                color = (100, 100, 200, 50)
                pygame.draw.circle(
                    self.surface, color,
                    (corner_x + 50, corner_y + 50), radius, 2
                )

    def generate_wallpaper(self, theme="default"):
        """生成壁纸"""
        # 绘制背景
        self.draw_background()
        
        # 绘制主要内容
        self.draw_linux_shortcuts()
        self.draw_coding_tips()
        self.draw_daily_reminders()
        
        # 绘制页眉页脚
        self.draw_header_footer()
        
        # 绘制装饰元素
        self.draw_decorative_elements()
        
        # 保存图片
        pygame.image.save(self.surface, self.output_path)
        print(f"壁纸已生成: {self.output_path}")

def main():
    parser = argparse.ArgumentParser(description='生成Linux系统信息壁纸')
    parser.add_argument('--output', '-o', default='/home/w-dingyu01@uos/dy外网/My_model/pygame测试/wallpaper.png',
                       help='输出文件路径')
    parser.add_argument('--width', '-W', type=int, default=1920,
                       help='壁纸宽度')
    parser.add_argument('--height', '-H', type=int, default=1080,
                       help='壁纸高度')
    parser.add_argument('--theme', '-t', default='default',
                       choices=['default', 'dark', 'blue'],
                       help='壁纸主题')
    
    args = parser.parse_args()
    
    # 创建生成器并生成壁纸
    generator = WallpaperGenerator(args.output, args.width, args.height)
    generator.generate_wallpaper(args.theme)

if __name__ == "__main__":
    main()